package com.yswl.pri.common;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.yswl.pri.R;

import yswl.com.klibrary.base.MFragment;

/**
 * Created by kangpAdministrator on 2018/5/17 0017.
 * Emial kangpeng@yunhetong.net
 */

public class ToolbarMFragment extends MFragment {
    @Override
    public boolean getFragmentMenuSetting() {
        return true;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        if (getFragmentMenuSetting())
            initToolbar(view);
    }

    Toolbar toolbar;

    public void initToolbar(View view) {
        toolbar = view.findViewById(R.id.toolbar);
        if (toolbar == null) return;
        getMActivity().setSupportActionBar(toolbar);
        setToolbarTitle(getToolbarTitle());
        setCustomViewToolbar(getCustomView());
    }

    public Toolbar getToolbar() {
        return toolbar;
    }

    protected void setToolbarTitle(String toolbarTitle) {
        if (getMActivity().getSupportActionBar() == null)
            throw new NullPointerException("布局文件有木有写ToolBar啊");
        if (TextUtils.isEmpty(toolbarTitle)) return;
        getMActivity().getSupportActionBar().setTitle(toolbarTitle);
    }

    protected String getToolbarTitle() {
        return "";
    }

    protected View getCustomView() {
        return null;
    }

    protected void setCustomViewToolbar(View view) {
        ActionBar bar = getMActivity().getSupportActionBar();
        if (bar == null)
            throw new NullPointerException("布局文件有木有写ToolBar啊");
        if (view != null) {
            bar.setCustomView(view);
            bar.setDisplayShowCustomEnabled(true);
            bar.setDisplayShowTitleEnabled(false);
        }
    }


}
